package com.bangcommunity.bbframe.sdm.datasource;

import com.bangcommunity.bbframe.common.utils.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author tanghc
 * @date 18/6/20
 */
public class MultDataSourceKey {
    static Logger logger = LoggerFactory.getLogger(MultDataSourceKey.class);
    private static final ThreadLocal<String> DB_KEY = new ThreadLocal();
    private static final ThreadLocal<String> Force_DB_KEY = new ThreadLocal();
    public static void setDataSourceKey(String key){
        logger.info("setDataSourceKey to : {} ",key);
        DB_KEY.set(key);
    }
    public static void clearDataSourceKey(){
        logger.info("clearDataSourceKey ");
        DB_KEY.remove();
        Force_DB_KEY.remove();
    }
    public static void forceSwichDataSourceKey(String key){
        logger.info("setDataSourceKey to : {} ",key);
        Force_DB_KEY.set(key);
    }

    public static String getDataSourceKey() {
        String key = Force_DB_KEY.get();
        if(StringUtils.isNotBlank(key)){
            return key;
        }
        return DB_KEY.get();
    }
}
